package com.cn.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.cn.bean.EnumOrder;
import com.cn.dao.OrderDAO;

public class OrderDAOImpl extends BaseHibernateDAOImpl implements OrderDAO {

	/**
	 * 根据re_id查找订单
	 * 
	 * @param re_id
	 *            用户Id
	 * @return 该用户相关的所有订单
	 */
	public List getAllOrders(int re_id) {
		try {
			String hql = "from Order as order where order.resident.id=" + re_id + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	
	/**
	 * 根据pro_id查找订单
	 * 
	 * @param pro_id
	 *            供应商Id
	 * @return 供应商的所有订单
	 */
	public List getAllOrdersByProvider(int pro_id) {
		try {
			String hql = "from Order as order where order.provider.id=" + pro_id + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	public List getRankByProvider(int pro_id) {
		try {
			String hql = "select AVG(rank) from Order as average where order.provider.id=" + pro_id;
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			System.out.println(queryObject.uniqueResult());
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	/**
	 * 根据re_id查找订单
	 * 
	 * @param re_id
	 *            用户Id
	 * 
	 * @return 该用户相关的未接单订单
	 */
	public List getWaitTakeOrder(int re_id) {
		try {
			String hql = "from Order as order where order.resident.id=" + re_id + " and order.state='"
					+ EnumOrder.ORDER_WAIT_TAKE + "'" + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	/**
	 * 根据pro_id查找订单
	 * 
	 * @param pro_id
	 *            供应商Id
	 * 
	 * @return 供应商所有的未接单订单
	 */
	public List getWaitTakeOrderByProvider(int pro_id) {
		try {
			String hql = "from Order as order where order.provider.id=" + pro_id + " and order.state='"
					+ EnumOrder.ORDER_WAIT_TAKE + "'" + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	

	/**
	 * 根据re_id查找订单
	 * 
	 * @param re_id
	 *            用户Id
	 * 
	 * @return 该用户相关的未确认订单
	 */
	public List getWaitConfirmOrder(int re_id) {
		try {
			String hql = "from Order as order where order.resident.id=" + re_id + " and order.state='"
					+ EnumOrder.ORDER_WAIT_COMFIRM + "'" + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据re_id查找订单
	 * 
	 * @param re_id
	 *            用户Id
	 * 
	 * @return 该用户相关的未评价订单
	 */
	public List getWaitRemarkOrder(int re_id) {
		try {
			String hql = "from Order as order where order.resident.id=" + re_id + " and order.state='"
					+ EnumOrder.ORDER_WAIT_REMARK + "'" + " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据re_id查找订单
	 * 
	 * @param re_id
	 *            用户Id
	 * 
	 * @return 该用户相关的已完成订单
	 */
	public List getCompletedOrder(int re_id) {
		try {
			String hql = "from Order as order where order.resident.id=" + re_id + " and order.state='"
					+ EnumOrder.ORDER_WAIT_REMARK + "' or  order.state='" + EnumOrder.ORDER_REMARKED + "'"
					+ " order by time DESC";
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(hql);
			List<Object> list = queryObject.list();
			getSession().getTransaction().commit();
			return list;
		} catch (RuntimeException re) {
			throw re;
		}
	}

	/**
	 * 根据id查找order
	 * 
	 * @param id
	 * @return order对象
	 */
	public Object getOrderById(int id) {
		try {
			String queryString = "from Order as order where id =" + id;
			getSession().beginTransaction();
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			getSession().getTransaction().commit();
			if (list.size() == 0) {
				return null;
			}
			return list.get(0);
		} catch (RuntimeException re) {
			throw re;
		}
	}
}
